/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.panels;

import data.Resource;
import gui.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import maiaclient.interfaces.IResourceListener;

/**
 *
 * @author groep 11
 * @date 2007-2008
 */
public class UpperBar extends JPanel implements IResourceListener {

    // Final Fiels
    final String PATTERNBAR = "src/img/patternBar.gif";
    final String IMGFOOD = "src/img/food.jpg";
    final String IMGWOOD = "src/img/wood.jpg";
    final String IMGSTONE = "src/img/stone.jpg";
    final String IMGIRON = "src/img/iron.jpg";
    final String IMGGOLD = "src/img/gold.jpg";
    // Fields
    private Image pattern;
    private AbstrWorld parent;
    private JButton btnContextSwitch;
    private JLabel lblFood;
    private JLabel lblWood;
    private JLabel lblStone;
    private JLabel lblIron;
    private JLabel lblGold;

    public UpperBar(AbstrWorld parent) {


        this.parent = parent;
        pattern = Toolkit.getDefaultToolkit().getImage(PATTERNBAR);

        // Fixed height
        setPreferredSize(new Dimension(0, 30));
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

        // When parent object is MaiaWorld we can load resources
        if (parent instanceof MaiaWorld) {
            addItems();
        }

        add(Box.createHorizontalGlue());
        JButton btnQuit = new JButton("Quit");
        btnQuit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                btnQuitClicked(e);
            }
            });

        add(btnQuit);
        add(Box.createHorizontalStrut(20));
        // ask resources 
        if (parent instanceof MaiaWorld) {
            parent.getMaia().addResourceListener(this);
            try {
                parent.getMaia().requestResources();
            } catch (IOException ex) {
                Logger.getLogger(UpperBar.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void addItems() {

        // Food
        JLabel lblImgFood = new JLabel(new ImageIcon(IMGFOOD));
        setLblFood(new JLabel("0"));
        getLblFood().setForeground(Color.WHITE);
        getLblFood().setOpaque(true);
        getLblFood().setBackground(Color.BLACK);
        getLblFood().setBorder(BorderFactory.createMatteBorder(0, 10, 0, 10, Color.black));
        add(Box.createHorizontalStrut(20));
        add(lblImgFood);
        add(getLblFood());
        add(Box.createHorizontalStrut(20));

        // Wood
        JLabel lblImgWood = new JLabel(new ImageIcon(IMGWOOD));
        setLblWood(new JLabel("0"));
        getLblWood().setBorder(BorderFactory.createMatteBorder(0, 10, 0, 10, Color.black));
        getLblWood().setForeground(Color.WHITE);
        getLblWood().setOpaque(true);
        getLblWood().setBackground(Color.BLACK);
        add(lblImgWood);
        add(getLblWood());
        add(Box.createHorizontalStrut(20));

        // stone
        JLabel lblImgStone = new JLabel(new ImageIcon(IMGSTONE));
        setLblStone(new JLabel("0"));
        getLblStone().setBorder(BorderFactory.createMatteBorder(0, 10, 0, 10, Color.black));
        getLblStone().setForeground(Color.WHITE);
        getLblStone().setOpaque(true);
        getLblStone().setBackground(Color.BLACK);
        add(lblImgStone);
        add(getLblStone());
        add(Box.createHorizontalStrut(20));

        // Iron
        JLabel lblImgIron = new JLabel(new ImageIcon(IMGIRON));
        setLblIron(new JLabel("0"));
        getLblIron().setBorder(BorderFactory.createMatteBorder(0, 10, 0, 10, Color.black));
        getLblIron().setForeground(Color.WHITE);
        getLblIron().setOpaque(true);
        getLblIron().setBackground(Color.BLACK);
        add(lblImgIron);
        add(getLblIron());
        add(Box.createHorizontalStrut(20));

        //Gold
        JLabel lblImgGold = new JLabel(new ImageIcon(IMGGOLD));
        setLblGold(new JLabel("0"));
        getLblGold().setBorder(BorderFactory.createMatteBorder(0, 10, 0, 10, Color.black));
        getLblGold().setForeground(Color.WHITE);
        getLblGold().setOpaque(true);
        getLblGold().setBackground(Color.BLACK);

        add(lblImgGold);
        add(getLblGold());


        add(Box.createHorizontalStrut((parent.getSize().width / 2) - 400));

        // context-switch knop
        btnContextSwitch = new JButton("Show World");
        btnContextSwitch.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                btnSwitchView(e);
            }
            });

        add(btnContextSwitch);

    }

    public void btnSwitchView(java.awt.event.ActionEvent evt) {
        if (parent.getWorldView() == false) {
            parent.setWorldView(true);
            parent.ChangeViewToWorld();
            btnContextSwitch.setText("Show Village");


        } else {
            parent.setWorldView(false);
            parent.ChangeViewToVillage();
            btnContextSwitch.setText("Show World");
        }
    }

    public void btnQuitClicked(java.awt.event.ActionEvent evt) {
        if (JOptionPane.showConfirmDialog(parent, "Are you sure you want to quit Maia? ",
                "Leave Maia", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            try {
                parent.getMaia().getMaiaClient().closeConnectionOut();
                System.exit(0);
            } catch (IOException ex) {
                Logger.getLogger(UpperBar.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (pattern != null) {
            Graphics2D g2 = (Graphics2D) g.create();
            Insets inset = this.getInsets();
            ImageIcon image = new ImageIcon(pattern);
            Rectangle clippingRegion = new Rectangle(this.getWidth() - (inset.left + inset.right), this.getHeight() - (inset.top + inset.bottom));

            g2.setClip(inset.left, inset.top, (int) clippingRegion.getWidth(), (int) clippingRegion.getHeight());

            int xRepeat = 0;
            int yRepeat = 0;

            yRepeat = (int) Math.ceil(clippingRegion.getHeight() / image.getIconHeight());
            xRepeat = (int) Math.ceil(clippingRegion.getWidth() / image.getIconWidth());

            for (int i = 0; i <= yRepeat; i++) {
                for (int j = 0; j <= xRepeat; j++) {
                    image.paintIcon(this, g2, j * image.getIconWidth() + inset.left, i * image.getIconHeight() + inset.top);
                }
            }

            g2.dispose();
        }
    }

    public void updateResources(Vector<Resource> resources) {

        for (Resource resource : resources) {

            if (resource.getResourceTypeName().equals("Food")) {
                if (resource.getAmount() != -1) {
                    getLblFood().setText("" + resource.getAmount());
                } else {
                    getLblFood().setEnabled(false);
                }
            }
            if (resource.getResourceTypeName().equals("Wood")) {
                if (resource.getAmount() != -1) {
                    getLblWood().setText("" + resource.getAmount());
                } else {
                    getLblWood().setEnabled(false);
                }
            }
            if (resource.getResourceTypeName().equals("Stone")) {
                if (resource.getAmount() != -1) {
                    getLblStone().setText("" + resource.getAmount());
                } else {
                    getLblStone().setEnabled(false);
                }
            }
            if (resource.getResourceTypeName().equals("Gold")) {
                if (resource.getAmount() != -1) {
                    getLblGold().setText("" + resource.getAmount());
                } else {
                    getLblGold().setEnabled(false);
                }
            }
            if (resource.getResourceTypeName().equals("Iron")) {
                if (resource.getAmount() != -1) {
                    getLblIron().setText("" + resource.getAmount());
                } else {
                    getLblIron().setEnabled(false);
                }
            }

        }

    }

    public JLabel getLblFood() {
        return lblFood;
    }

    public JLabel getLblWood() {
        return lblWood;
    }

    public JLabel getLblStone() {
        return lblStone;
    }

    public JLabel getLblIron() {
        return lblIron;
    }

    public JLabel getLblGold() {
        return lblGold;
    }

    public void setLblFood(JLabel lblFood) {
        this.lblFood = lblFood;
    }

    public void setLblWood(JLabel lblWood) {
        this.lblWood = lblWood;
    }

    public void setLblStone(JLabel lblStone) {
        this.lblStone = lblStone;
    }

    public void setLblIron(JLabel lblIron) {
        this.lblIron = lblIron;
    }

    public void setLblGold(JLabel lblGold) {
        this.lblGold = lblGold;
    }
}
